package de.fhkl.gatav.ut.localimageops_1;

import de.fhkl.gatav.ut.localimageops_1.R;
import de.fhkl.gatav.ut.localimageops_1.ImageWorkbenchView;

import java.io.FileNotFoundException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class LocalImageOps_1Activity extends Activity {

	Bitmap bmp = null;
	ImageWorkbenchView myImageWorkBenchView = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		myImageWorkBenchView = (ImageWorkbenchView) findViewById(R.id.myImageWorkbenchView);
		
		String toastMsg = getString(R.string.hello_and_hints);
		Toast t = Toast.makeText(this, toastMsg, Toast.LENGTH_SHORT);
		t.show();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	getMenuInflater().inflate(R.menu.main_optionsmenu, menu);
    	return true;
    }
 
    @Override    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	case R.id.id_optionsmenue_brightness:
    		myImageWorkBenchView.setMode( ImageWorkbenchView.MODE_BRIGHT );
    		break;
    		
    	case R.id.id_optionsmenue_contrast:
    		myImageWorkBenchView.setMode( ImageWorkbenchView.MODE_CONTRAST );
    		break;
    		
    	case R.id.id_optionsmenue_sharpen:
    		myImageWorkBenchView.setMode( ImageWorkbenchView.MODE_SHARPEN );
    		break;
    		
    	case R.id.id_optionsmenue_smoothen:
    		myImageWorkBenchView.setMode( ImageWorkbenchView.MODE_BLUR );
    		break;
    		
    	case R.id.id_optionsmenue_save:
    		Bitmap saveBmp = null;
			saveBmp = myImageWorkBenchView.getImage();
    		
			if (saveBmp != null) {
				ContentValues contentValues = new ContentValues(3);
				contentValues.put(Media.DISPLAY_NAME, "ImageProcessing");

				Uri imageFileUri = getContentResolver().insert(
						Media.EXTERNAL_CONTENT_URI, contentValues);

				try {
					OutputStream imageFileOS = 
						getContentResolver().openOutputStream(imageFileUri);

					saveBmp.compress(CompressFormat.JPEG, 90, imageFileOS);

					Toast t = Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT);
					t.show();

				} catch (FileNotFoundException e) {
					Log.v("EXCEPTION", e.getMessage());
				}
			}
    		break;
    		
    	case R.id.id_optionsmenue_load:
			Intent choosePictureIntent = new Intent( 
					Intent.ACTION_PICK,
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(choosePictureIntent, 0);
    		break;
    		
    	case R.id.id_optionsmenue_undo:
    		if (bmp != null) {
    			myImageWorkBenchView.setImage(bmp);
    		}
    		break;
    		
    	default:
    		return super.onOptionsItemSelected(item);
    	}
    	
    	return true;
    }
    
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);

		if (resultCode == RESULT_OK) {
			Uri imageFileUri = intent.getData();
			Display currentDisplay = getWindowManager().getDefaultDisplay();

			float displayWidth = currentDisplay.getWidth();
			float displayHeight = currentDisplay.getHeight();
			Log.v("OACR", "width "+displayWidth+" height "+displayHeight);

			try {
				bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri));
				
				Log.v("OACR", "bmpwidth "+bmp.getWidth()+" bmpheight "+bmp.getHeight());
				
				//Load resampled image into background
				myImageWorkBenchView.setImage(bmp);
			
			} catch (FileNotFoundException e) {
				Log.v("ERROR", e.toString());
			}
		}
	}
}